/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmChangePassword.java
 *
 * Created on Jul 28, 2011, 11:47:52 AM
 */

package khmhappointmentsystem;

import org.jdesktop.application.Action;

/**
 *
 * @author mbelisle
 */
public class FrmChangePassword extends javax.swing.JInternalFrame {
    int tryCounter = 0;


    /** Creates new form FrmChangePassword */
    public FrmChangePassword() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        txtCurrent = new javax.swing.JPasswordField();
        txtPassword = new javax.swing.JPasswordField();
        txtRepeat = new javax.swing.JPasswordField();

        setClosable(true);
        setIconifiable(true);
        setResizable(true);
        setTitle("Change Password");

        jLabel1.setText("Current Password:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("New Password:");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("Repeat Password:");
        jLabel3.setName("jLabel3"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(FrmChangePassword.class, this);
        jButton1.setAction(actionMap.get("Cancel")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N

        jButton2.setAction(actionMap.get("Save")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N

        txtCurrent.setName("txtCurrent"); // NOI18N

        txtPassword.setName("txtPassword"); // NOI18N

        txtRepeat.setName("txtRepeat"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(jButton1))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtRepeat, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                            .addComponent(txtCurrent, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                            .addComponent(txtPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE))))
                .addGap(52, 52, 52))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtCurrent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtRepeat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-356)/2, (screenSize.height-186)/2, 356, 186);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void Cancel()
    {
        Utilities.showCancelScreen(this);
    }

    @Action
    public void Save()
    {
        String password = String.valueOf(txtCurrent.getPassword()).trim();
        if (User.logIn(User.getUserName(), password))
        {
            String newPassword = String.valueOf(txtPassword.getPassword());
            String confirm = String.valueOf(txtRepeat.getPassword());
            if (newPassword.equals(confirm))
            {
                newPassword = EncryptionHandler.encryptPassword(newPassword);
                if (User.changePassword(User.getUserName(), newPassword))
                {
                    String message = "Your password was successfully changed.";
                    Utilities.showInfoMessage(rootPane, message);
                    return;
                }
                else // If it doesn't get added tell the user something went wrong.
                {
                    String message = "An error occurred while changing your password.\n"
                                     + "Kindly verify your information and try again.\n"
                                     + "If the problem persists, kindly contact your system administrator.";
                    Utilities.showErrorMessage(rootPane, message);
                }
                return;
            }
            else
            {
                String message = "The passwords you have entered doesn't match.\n"
                                 + "Kindly try again.";
                Utilities.showWarningMessage(rootPane, message);
                return;
            }
        }
        else
        {
            String message = "The password you have entered doesn't match your currently saved password.\n"
                             + "Kindly try again.";
            Utilities.showWarningMessage(rootPane, message);
            tryCounter++;
            if (tryCounter >= 3)
            {
                message = "You have failed to enter the correct password three times.\n"
                          + "The application will now exit.";
                Utilities.showErrorMessage(rootPane, message);
                khmhappointmentsystem.Main.getApplication().exit();
            }
            return;
        }

    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField txtCurrent;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JPasswordField txtRepeat;
    // End of variables declaration//GEN-END:variables

}
